<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserProcess;
use App\Models\ProcessesArchive;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Actions\Grid\SetProcessesArchives;

class UserProcessController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserProcess::with(['user', 'workshop']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('workshop.title', admin_trans('user-process.fields.workshop_title'));
            $grid->column('user.name', admin_trans('user-process.fields.user_name'));
            $grid->column('processes_archives_ids', admin_trans('user-process.fields.processes_archives_title'))->display(function($processes_archives_ids) {
                $title = '';

                if ($processes_archives_ids) {
                    $processes_archive = ProcessesArchive::whereIn('id', $processes_archives_ids)->get();

                    if ($processes_archive) {
                        foreach ($processes_archive as $key => $value) {
                            if ($key == 0) {
                                $title = $value['title'];
                            } else {
                                $title .= ",{$value['title']}";
                            }
                        }

                    }
                }

                return $title;
            });
            $grid->column('updated_at')->sortable();
//            $grid->actions([new SetProcessesArchives()]);
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, UserProcess::with(['user', 'workshop']), function (Show $show) {
            $show->field('id');
            $show->field('workshop.title', admin_trans('user-process.fields.workshop_title'));
            $show->field('user.name', admin_trans('user-process.fields.user_name'));
            $show->field('processes_archives_ids')->as(function ($processes_archives_ids) {
                $processes_archive = ProcessesArchive::whereIn('id', $processes_archives_ids)->get();
                
                $title = '';

                if ($processes_archive) {
                    foreach ($processes_archive as $key => $value) {
                        if ($key == 0) {
                            $title = $value['title'];
                        } else {
                            $title .= ",{$value['title']}";
                        }
                    }

                }

                return $title;
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserProcess(), function (Form $form) {
            $form->display('id');
            $processes_archive = ProcessesArchive::where('status', DEFAULT_TRUE)->where('type', PROCESSES_IN)->pluck('title', 'id');

            $form->checkbox('processes_archives_ids', admin_trans('user-process.fields.processes_archives_title'))->options($processes_archive);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
